﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ServiceGUI
{
    public partial class AdminControl : UserControl
    {
        private WorkBase.ServerClient _client;

        public delegate void Logout();
        private Logout _logout;

        public AdminControl(WorkBase.ServerClient client, Logout logout)
        {
            InitializeComponent();

            _logout = logout;
            _client = client;

            _client.GetUsersCompleted += new EventHandler<WorkBase.GetUsersCompletedEventArgs>(_client_GetUsersCompleted);
            _client.DeleteWorkerCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(_client_DeleteWorkerCompleted);
            _client.SetPostCompleted += new EventHandler<WorkBase.SetPostCompletedEventArgs>(_client_SetPostCompleted);
            _client.SetPasswordCompleted += new EventHandler<WorkBase.SetPasswordCompletedEventArgs>(_client_SetPasswordCompleted);
            _client.RegistryLoginCompleted += new EventHandler<WorkBase.RegistryLoginCompletedEventArgs>(_client_RegistryLoginCompleted);

            _client.GetUsersAsync();
        }

        void _client_RegistryLoginCompleted(object sender, WorkBase.RegistryLoginCompletedEventArgs e)
        {
            _client.GetUsersAsync();
        }

        void _client_SetPasswordCompleted(object sender, WorkBase.SetPasswordCompletedEventArgs e)
        {
        }

        void _client_SetPostCompleted(object sender, WorkBase.SetPostCompletedEventArgs e)
        {
        }

        void _client_DeleteWorkerCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            _client.GetUsersAsync();
        }

        void _client_GetUsersCompleted(object sender, WorkBase.GetUsersCompletedEventArgs e)
        {
            UserList.Items.Clear();
            foreach (var user in from u in e.Result select u.Name)
            {
                UserList.Items.Add(user);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _logout();
        }

        // добавление нового юзверя
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                var name = UserName.Text;
                var post = UserPost.Text;
                var salary = Int32.Parse(UserSalary.Text);
                var password = UserPassword.Text;

                _client.IsValidLoginPasswordCompleted += new EventHandler<WorkBase.IsValidLoginPasswordCompletedEventArgs>(_client_IsValidLoginPasswordCompleted);
                _client.RegistryLoginAsync(name, password,post,salary);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void _client_IsValidLoginPasswordCompleted(object sender, WorkBase.IsValidLoginPasswordCompletedEventArgs e)
        {
            _client.GetUsersAsync();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            _client.DeleteWorkerAsync(CurrentUserName.Text);
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            _client.SetPostAsync(CurrentUserName.Text, CommonDataBox.Text);
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            _client.SetPasswordAsync(CurrentUserName.Text, CommonDataBox.Text);
        }
    }
}
